<!DOCTYPE html>
<!-- LaTeX to MathML conversion script. Uses MathJax - licensed under Apache License. 
Adapted from an example by Davide Cervone in: http://docs.mathjax.org/en/latest/typeset.html -->

<!-- (c) 2014 Andres Raba, GNU GPL v.3. -->

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>LaTeX to MathML conversion</title>
<meta charset="utf-8"/>

<script type="text/x-mathjax-config">
//<![CDATA[
  MathJax.Hub.Queue([alert, 'Listening']);
  (function () {
    // This exports ConvertToMML() that takes a string of LaTeX 
    // and writes the resulting MathML to the hashmap window.mathml,
    // using LaTeX string as key.
    var QUEUE = MathJax.Hub.queue,  // shorthand for the queue
        mathcell = "mathcell";  // element id of the conversion cell
    window.mathml = {};  // this will hold the results
    window.ConvertToMML = function (TeX) {
      try {
        QUEUE.Push(function () {  // overwrite the conversion cell
          document.getElementById(mathcell).innerHTML = TeX;
        });
        QUEUE.Push(["Typeset", MathJax.Hub, mathcell]);  // retypeset the cell
        QUEUE.Push(function () {  // get the jax and extract MathML from it
          var math = MathJax.Hub.getAllJax(mathcell)[0];
          if (math) {
            toMathML(math, function (mml) {
              window.mathml[TeX] = mml;
            });
          } 
          else {  // TeX input was probably faulty, make it stand out
            window.mathml[TeX] = "<span class='faulty'>" + TeX + "</span>";
          }
        });
        //QUEUE.Push([alert, 'Retypeset']);  // signal the end of single conversion
      } catch(error) {
        console.log(error);
        document.location.reload(); // MathJax won't resurrect otherwise
      }
    }
    window.ConversionEnd = function () {  // signal the end of all conversions
      try {                               // after which it's safe to exit
        QUEUE.Push([alert, 'End']);
      } catch(error) {
        console.log(error);
        document.location.reload();
      }
    }
    function toMathML(jax,callback) {  // extract MathML from jax
      var mml;
      try {
        mml = jax.root.toMathML("");
      } catch(err) {
        if (!err.restart) { throw err } // an actual error
        return MathJax.Callback.After([toMathML,jax,callback],err.restart);
      }
      MathJax.Callback(callback)(mml);
    }
  })();
//]]>
</script>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full" type="text/javascript"></script>
</head>

<body>

<!-- The conversion cell. Contents will be overwritten each time ConvertToMML() is called. -->
<div id="mathcell"> 
\[ \mathrm{e^{i\pi}} + 1 = 0 \]
</div>

</body>
</html>
